unit UGumba;

interface

uses SysUtils, Classes, Windows, Math, UMapObjekte, UPictureName, USound;

type
  TGumba = class(TMapObject) // 5
  private
    MoveLeft: boolean;
    enabled: boolean;

  public
    constructor Create(Id: integer); Override;
    function getCollisionAtPixel(Game: TObject; X, Y: integer): integer;
      Override;
    procedure Action(Game: TObject); Override;
    procedure Disable();
  end;

implementation

uses UGame, UPlayer, UMap;
{ TGumba }

procedure TGumba.Action(Game: TObject);
var
  Objects: TMapObjekts;
  i: integer;
  Collision: boolean;
begin
  if self.enabled then
  begin
    Objects := TGame(Game).getMap.GetMapObjekte;
    Collision := False;

    for i := 0 to High(Objects) do
    begin
      if (self.MoveLeft) then
      begin
        if (self.StartX = Objects[i].endex) then
          if (Objects[i].EndeY > self.EndeY - 1) and
            (self.EndeY - 1 > Objects[i].StartY) and not(Objects[i] is TPlayer)
            then
            Collision := True;
      end
      else
      begin
        if (self.endex = Objects[i].StartX) then
          if (Objects[i].EndeY > self.EndeY - 1) and
            (self.EndeY - 1 > Objects[i].StartY) and not(Objects[i] is TPlayer)
            then
            Collision := True;
      end;
    end;

    if Collision then
      self.MoveLeft := not self.MoveLeft;

    if self.MoveLeft then
    begin
      dec(self.FStartX);
      dec(self.FEndex);
    end
    else
    begin
      inc(self.FStartX);
      inc(self.FEndex);
    end;

    if Assigned(self.OnMove) then
      self.OnMove(self);
  end;
end;

constructor TGumba.Create(Id: integer);
begin
  self.Id := Id;
  self.Num := 3;
  enabled := True;
  FPictureName := a_Gumba;
  MoveLeft := True;
  self.affgravity := True;
end;

procedure TGumba.Disable;
begin
  self.enabled := False;
  self.FEndex := self.StartX;
  self.FEndeY := self.StartY;
  self.FPictureName := null;
end;

function TGumba.getCollisionAtPixel(Game: TObject; X, Y: integer): integer;
var
  vgame: TGame;
begin
  vgame := TGame(Game);
  if self.enabled then
  begin
    if (((vgame.getTexturePack.GetNamedPicture(self.PictureName)
            .Bitmap.Canvas.Pixels[X, Y] = RGB(255, 0, 0)) and
          (vgame.getTexturePack.GetNamedPicture(self.PictureName)
            .Bitmap.Canvas.Pixels[X, Y] <> -1))) then
    begin
      // Von oben getroffen
      vgame.PlaySound(HIT.Pos);
      // Objekt entfernen
      result := 1;
      self.Disable;
      if Assigned(self.OnMove) then
        self.OnMove(self);
      if vgame.IsClient then
        vgame.objectmove(self);
    end
    else
    begin
      if (((vgame.getTexturePack.GetNamedPicture(self.PictureName)
              .Bitmap.Canvas.Pixels[X, Y] = RGB(250, 0, 250)) or
            (vgame.getTexturePack.GetNamedPicture(self.PictureName)
              .Bitmap.Canvas.Pixels[X, Y] = -1))) then
        result := 0
      else
        result := 2;
        self.MoveLeft := not self.MoveLeft;
    end;
  end
  else
    result := 0;
end;

end.
